// store/menu.ts
import { defineStore } from "pinia";

export const useMenuStore = defineStore("menu", {
  state: () => ({
    menuList: [] as any[], // 保存菜单树，用于渲染侧边栏
  }),
  actions: {
    setMenuList(menuList: any[]) {
      const filterMenu = (menus: any[]): any[] => {
        return menus
          .filter((item) => item.meta?.showMenu !== false) // 显式为 false 才过滤
          .map((item) => {
            if (item.children && Array.isArray(item.children)) {
              return {
                ...item,
                children: filterMenu(item.children),
              };
            }
            return { ...item };
          });
      };

      this.menuList = filterMenu(menuList);
    },
    clearMenuList() {
      this.menuList = [];
    },
  },
  // 持久化（可选）：如果你希望刷新后菜单还在（但通常需要重新登录）
  // persist: true
});
